#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2020/12/22 11:10
    Desc  :
--------------------------------------
"""
from servers.models.VariableModel import Variable
from servers.servers.BaseServices import BaseServices


class VariableServices(BaseServices):

    def __init__(self, document = Variable):
        super(VariableServices, self).__init__(document)

    def find_by_any(self, **kwargs):
        """"""
        veriable = Variable.objects(**kwargs).first()

        return veriable.variableValue

    def add_veriable(self, **kwargs):
        variableName, projectId = kwargs.get('variableName'), kwargs.get('projectId')
        veriable = Variable.objects(variableName = variableName, projectId = projectId)
        if veriable.count() == 0:
            self.save(**kwargs)
        else:
            veriable.update(**kwargs)


def get_veriable_by_name(**kwargs):
    """
    通过变量名称获取变量值
    :param variableName: 变量名称
    :return:
    """

    variableName, projectId = kwargs.get('variableName'), kwargs.get('projectId')
    veriable = Variable.objects(variableName = variableName)

    if veriable.count() != 0:
        return veriable.first().variableValue

    return None


def get_veriable_by_id(id: str):
    """
    通过变量id获取变量值
    :param id: 变量名称
    :return:
    """
    veriable = Variable.objects(id = id)

    if veriable.count() != 0:
        return veriable.first().variableValue

    return None


def add_veriable(**kwargs):
    variableName, projectId = kwargs.get('variableName'), kwargs.get('projectId')
    veriable = Variable.objects(variableName = variableName, projectId = projectId)
    if veriable.count() == 0:
        Variable(**kwargs).save()
    else:
        veriable.update(**kwargs)
